package com.huangj.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.huangj.entity.HomeInfoEntity;
import com.huangj.fragments.lv2MapFragment;
import com.huangj.pro2_h2pop.HomeInfoActivity;
import com.huangj.pro2_h2pop.Lv4Item_PicActivity;
import com.huangj.pro2_h2pop.R;
import com.huangj.wedigt.MyListview;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HuangJ on 2016/10/4.16:38
 */
public class HomeInfoAdapter extends BaseAdapter {

    private Context context;
    private List<HomeInfoEntity.DataBean.SectionsBean> datas;
    private FragmentManager fragmentManager;

    private Lv3PicsAdapter lv3PicsAdapter;

    public HomeInfoAdapter(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
        this.datas = new ArrayList<>();
    }

    public void setDatas(List<HomeInfoEntity.DataBean.SectionsBean> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView != null){
            viewHolder = (ViewHolder) convertView.getTag();
        }else{
            viewHolder = new ViewHolder();
            //布局1
            if (getItemViewType(position) == 0){
                convertView = LayoutInflater.from(context).inflate(R.layout.homeinfo_lv1, null);
                //设置文字;
                viewHolder.tv_lv1top = (TextView) convertView.findViewById(R.id.tv_lv1top);
                viewHolder.tv_lv1bottom = (TextView) convertView.findViewById(R.id.tv_lv1bottom);
                //横向的ScrollView;
                viewHolder.hsv_ll = (LinearLayout) convertView.findViewById(R.id.hsv_ll);
                convertView.setTag(viewHolder);
            }
            //布局2；
            else if (getItemViewType(position) == 1){
                convertView = LayoutInflater.from(context).inflate(R.layout.homeinfo_lv2,null);
                viewHolder.tv_lv2top = (TextView) convertView.findViewById(R.id.tv_lv2top);
//                viewHolder.tv_lv2title = (TextView) convertView.findViewById(R.id.tv_lv2title);
                viewHolder.lv2_fl = (FrameLayout) convertView.findViewById(R.id.lv2_fl);
                viewHolder.gb_lv2 = (RadioGroup) convertView.findViewById(R.id.gb_lv2);
                convertView.setTag(viewHolder);
            }
            //布局3；
            else if (getItemViewType(position) == 2){
                convertView = LayoutInflater.from(context).inflate(R.layout.homeinfo_lv3,null);
                viewHolder.tv_lv3top = (TextView) convertView.findViewById(R.id.tv_lv3top);
                viewHolder.lv3_myListView = (MyListview) convertView.findViewById(R.id.iv3_mylistview);
                convertView.setTag(viewHolder);
            }
            //布局4；
            else if (getItemViewType(position) == 3){
                convertView = LayoutInflater.from(context).inflate(R.layout.homeinfo_lv4,null);
                viewHolder.tv_lv4top = (TextView) convertView.findViewById(R.id.tv_lv4top);
                viewHolder.tv_lv4author = (TextView) convertView.findViewById(R.id.tv_lv4author);
                viewHolder.tv_lv4topic = (TextView) convertView.findViewById(R.id.tv_lv4topic);
                viewHolder.tv_lv4summery = (TextView) convertView.findViewById(R.id.tv_lv4summery);
                viewHolder.tv_lv4count = (TextView) convertView.findViewById(R.id.tv_lv4count);
                viewHolder.iv_lv4 = (ImageView) convertView.findViewById(R.id.lv4_iv);
                viewHolder.lv4_tvpics = (TextView) convertView.findViewById(R.id.lv4_tvpics);
                convertView.setTag(viewHolder);
            }
        }
                //布局1；
        if (getItemViewType(position) == 0) {
            viewHolder.tv_lv1top.setText(datas.get(position).getTitle());
            viewHolder.tv_lv1bottom.setText(datas.get(position).getButton_text());
//            Log.d("print", "getView: "+datas.get(position).getButton_text());
            //动态设置图片加文字;
            final List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean> models = datas.get(position).getModels();
            viewHolder.hsv_ll.removeAllViews();
            for (int i = 0; i < models.size(); i++) {
                final  int index = i;
                LinearLayout linearLayout = new LinearLayout(context);
                linearLayout.setGravity(Gravity.CENTER);
                linearLayout.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                linearLayout.setLayoutParams(layoutParams);
                ImageView iv_lv1 = new ImageView(context);
                LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(220, 110);
                layoutParams1.bottomMargin = 10;
                iv_lv1.setLayoutParams(layoutParams1);
                Glide.with(context).load(models.get(i).getPhoto_url()).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_lv1);
                TextView tv1 = new TextView(context);
                tv1.setGravity(Gravity.CENTER);
                tv1.setText(models.get(i).getName());
                TextView tv2 = new TextView(context);
                tv2.setText(models.get(i).getName_en());
                tv2.setGravity(Gravity.CENTER);
                iv_lv1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context,HomeInfoActivity.class);
                        intent.putExtra("id",models.get(index).getId());
                        context.startActivity(intent);
                    }
                });
                linearLayout.addView(iv_lv1);
                linearLayout.addView(tv1);
                linearLayout.addView(tv2);
                viewHolder.hsv_ll.addView(linearLayout);
            }
        }
        //布局2
        else if(getItemViewType(position) == 1){
            viewHolder.tv_lv2top.setText(datas.get(position).getTitle());
            final List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean> models = datas.get(position).getModels();
//            viewHolder.tv_lv2title.setText(models.get(0).getTitle());

            viewHolder.gb_lv2.removeAllViews();
            for (int i = 0; i < models.size(); i++) {
                final int index = i ;
                RadioButton radioButton = new RadioButton(context);
                radioButton.setText(models.get(i).getDays_count()+"日行程");
                radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if (isChecked){
//                            Log.d("print", "onCheckedChanged: 点击了");
                            fragmentManager.beginTransaction().replace(R.id.lv2_fl, lv2MapFragment.getIntance(models.get(index))).commit();
                        }
                    }
                });
                viewHolder.gb_lv2.addView(radioButton);
            }
//            viewHolder.gb_lv2.getChildAt(0).performClick();
        }
        //布局3
        if (getItemViewType(position) == 2){
            viewHolder.tv_lv3top.setText(datas.get(position).getTitle());
            List<HomeInfoEntity.DataBean.SectionsBean.ModelsBean> models3 = datas.get(position).getModels();
            lv3PicsAdapter = new Lv3PicsAdapter(context);
            lv3PicsAdapter.setDatas(models3);
            viewHolder.lv3_myListView.setAdapter(lv3PicsAdapter);
        }
        //布局4
        else if (getItemViewType(position) == 3){
            viewHolder.tv_lv4top.setText(datas.get(position).getTitle());
            viewHolder.tv_lv4count.setText(datas.get(position).getButton_text());
            viewHolder.tv_lv4author.setText(datas.get(position).getModels().get(0).getUser().getName());
            viewHolder.tv_lv4topic.setText(datas.get(position).getModels().get(0).getTopic());
            viewHolder.tv_lv4summery .setText(datas.get(position).getModels().get(0).getDescription());
            viewHolder.lv4_tvpics.setText(datas.get(position).getModels().get(0).getContents_count());
            Glide.with(context).load(datas.get(position).getModels().get(0).getContents().get(0).getPhoto_url())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .thumbnail(0.1f)
                    .into(viewHolder.iv_lv4);
            viewHolder.iv_lv4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   Intent intent = new Intent(context, Lv4Item_PicActivity.class);
                    ArrayList<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.Contents> contents = (ArrayList<HomeInfoEntity.DataBean.SectionsBean.ModelsBean.Contents>) datas.get(position).getModels().get(0).getContents();
                    intent.putExtra("contents",contents);
                    context.startActivity(intent);
                }
            });
        }
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 4;
    }

    @Override
    public int getItemViewType(int position) {
        return datas.get(position).getType();
    }

    class ViewHolder {
            //目的地介绍;
            TextView tv_lv1top, tv_lv1bottom;
            LinearLayout hsv_ll;
            //经典路线
            TextView tv_lv2top;FrameLayout lv2_fl;
            RadioGroup gb_lv2;
            //旅行榜单
            TextView tv_lv3top;MyListview lv3_myListView;
            //相关氢游记;
        ImageView iv_lv4;
            TextView tv_lv4top,tv_lv4author,tv_lv4topic,tv_lv4summery,tv_lv4count,lv4_tvpics;
        }
}
